<?php
// PXFInstance::breakPoint($current_categories);

class ControllerAdmin extends PXFControllerAdmin
{
	static $tablinks = null;

	public function __construct()
	{ 
		self::$tablinks = array(
			array('controller' => 'ajax_contacts', 'action' => 'index',  'title' => 'Sin Responder'),
			array('controller' => 'ajax_contacts', 'action' => 'index_responsed',  'title' => 'Respondidas'),
			array('controller' => 'ajax_contacts', 'action' => 'index_all',  'title' => 'Todas'),
		);
		parent::__construct();
		$this->tpl->assign('ITEM_TYPE','contacts');
		$this->tpl->assign('tablinks',self::$tablinks);
	}

	public function index()
	{
		PXFConfigAdmin::$site['page_title'] = 'Consultas Sin Responder';
	  $mod_contacts = PXFModel::getModel('contact');
		$params = array();
		$params['condition'] = new PXFCondition('responsed_at','0000-00-00','=');
		$count = $mod_contacts->count($params);
		$paginator = new PXFPaginator($count,array());
		$this->tpl->assign('filter',3);
		$this->tpl->assign('paginator',$paginator);
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->display('ajax_contacts.list.tpl');
	}

	public function index_all()
	{
		PXFConfigAdmin::$site['page_title'] = 'Consultas';
		$mod_contacts = PXFModel::getModel('contact');
		$params = array();
		$count = $mod_contacts->count($params);
		$paginator = new PXFPaginator($count,array());
		$this->tpl->assign('paginator',$paginator);
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->assign('filter',2);
		$this->tpl->display('ajax_contacts.list.tpl');
	}

	public function index_responsed()
	{
		PXFConfigAdmin::$site['page_title'] = 'Consultas Respondidas';
		$mod_contacts = PXFModel::getModel('contact');
		$params = array();
		$params['condition'] = new PXFCondition('responsed_at','0000-00-00','=');
		$count = $mod_contacts->count($params);
		$paginator = new PXFPaginator($count,array());
		$this->tpl->assign('paginator',$paginator);
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->assign('filter',1);
		$this->tpl->display('ajax_contacts.list.tpl');
	}

	public function contacts_get_all()
	{
	  $mod_contacts = PXFModel::getModel('contact');
		$contacts = $mod_contacts->findAll();
		$count = $mod_contacts->count();
		$params = array();
		$paginator = new PXFPaginator($count,array());
		$params['limit'] = array($paginator->from,$paginator->disp);
		$params['order_desc'] = array('id');
		$contacts = $mod_contacts->findBy($params);
		$result = array();
		foreach($contacts as $contact)
		{
			$result[] = array(
								'id' => $contact->id,
								'mail' => $contact->mail,
								'name' => $contact->name,
								'content' => $contact->content,
								'created_at' => $contact->created_at,
								'responsed_at' => $contact->responsed_at
								);
		}
		echo pxf_json_encode($result);
	}

	public function contacts_get_unresponsed()
	{
	  $mod_contacts = PXFModel::getModel('contact');
		$params = array();
		$params['condition'] = new PXFCondition('responsed_at','0000-00-00','=');
		$count = $mod_contacts->count($params);
		$paginator = new PXFPaginator($count,array());
		$params['limit'] = array($paginator->from,$paginator->disp);
		$contacts = $mod_contacts->findBy($params);
		$result = array();
		foreach($contacts as $contact)
		{
			$result[] = array(
								'id' => $contact->id,
								'mail' => $contact->mail,
								'name' => $contact->name,
								'content' => $contact->content,
								'created_at' => $contact->created_at,
								'responsed_at' => $contact->responsed_at
								);
		}
		echo pxf_json_encode($result);
	}

	public function contacts_get_responsed()
	{
	  $mod_contacts = PXFModel::getModel('contact');
		$params = array();
		$params['condition'] = new PXFCondition('responsed_at','0000-00-00','!=');
		$count = $mod_contacts->count($params);
		$paginator = new PXFPaginator($count,array());
		$params['limit'] = array($paginator->from,$paginator->disp);
		$contacts = $mod_contacts->findBy($params);
		$result = array();
		foreach($contacts as $contact)
		{
			$result[] = array(
								'id' => $contact->id,
								'mail' => $contact->mail,
								'name' => $contact->name,
								'content' => $contact->content,
								'created_at' => $contact->created_at,
								'responsed_at' => $contact->responsed_at
								);
		}
		echo pxf_json_encode($result);
	}



	public function contact_mark()
	{
	  $id = PXFRequest::reqGetOr404('id'); 
	  $mod_contacts = PXFModel::getModel('contact');
	  $contact = $mod_contacts->find($id);
	  $contact->responsed_at = date('Y-m-d h:i:s');
	  $contact->save();
		echo pxf_json_encode(array('msg' => sprintf('La consulta de %s ha sido marcada como respondida',$contact->mail) ));
	}


}
